﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mitasoft.AdsShedule.Common;
using System.IO;

namespace mitasoft.AdsShedule.Business
{
    public class BusinessAccess
    {
        #region Singleton

        private BusinessAccess()
        {
            string path = AppCommon.GetTitledPath(StringTitles.ClientListFile);

            if (File.Exists(path))
                DataSet.ReadXml(path);
        }

        private static BusinessAccess _instance;
        public static BusinessAccess Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new BusinessAccess();

                return _instance;
            }
        }

        private Business.AdDS _dataSet;
        public Business.AdDS DataSet
        {
            get
            {
                if (_dataSet == null)
                    _dataSet = new AdDS();


                return _dataSet;
            }
            set
            {
                _dataSet = value;
            }
        }

        public void SaveDataSet()
        {
            string path = AppCommon.GetTitledPath(StringTitles.ClientListFile);

            if (File.Exists(path))
                File.Delete(path);

            DataSet.WriteXml(path);
        }

        #endregion


        #region Appoiments

        public List<AppoimentButtonInfo> GetExistingAppoiments(IntervalType intervalType)
        {
            var appoimentTable = Business.BusinessAccess.Instance.DataSet.Appoiment;

            return appoimentTable
                .Where(item =>
                    item.Data.Year == Common.AppCommon.SelectedMonth.Year
                    && item.Data.Month == Common.AppCommon.SelectedMonth.Month
                //&& item.Interval == intervalType.ToString()
                    )
                .Select(item => new AppoimentButtonInfo
                {
                    Data = item.Data,
                    InvervalType = (IntervalType)Enum.Parse(typeof(IntervalType), intervalType.ToString()),
                    //VoiceAssocID = Convert.ToInt32(item.IDVoiceAssoc)
                })
                .ToList();
        }
        public AppoimentButtonInfo GetAppoimentButtonInfo(DateTime date, IntervalType intervalType)
        {
            var appoimentVoiceClientAssoc = DataSet.Appoiment
                   .Where(item => item.Data.Date.Equals(date.Date)
                       && item.Interval.Equals(intervalType.ToString()))
                   .Select(item => item.IDVoiceAssoc)
                   .ToList();

            AppoimentButtonInfo buttonInfo = new AppoimentButtonInfo();
            buttonInfo.Data = date;
            buttonInfo.InvervalType = intervalType;
            buttonInfo.VoiceAssocID = appoimentVoiceClientAssoc;
            return buttonInfo;
        }
        public string GetAppoimentInfo(DateTime date, IntervalType intervalType)
        {
            var appoimentVoiceClientAssoc = DataSet.Appoiment
                               .Where(item => item.Data.Date.Equals(date.Date)
                                   && item.Interval.Equals(intervalType.ToString()))
                               .ToList();

            StringBuilder stringBuilder = new StringBuilder();
            foreach (var app in appoimentVoiceClientAssoc)
            {
                stringBuilder.AppendLine(GetVoiceClientAssociations(app.IDVoiceAssoc).Description);
            }

            return stringBuilder.ToString();
        }

        public void SaveAppoimentButtonInfoToData(AppoimentButtonInfo buttonInfo)
        {
            if (buttonInfo != null)
            {

                //Step 1: Delete
                DeleteAppoiment(buttonInfo);

                //Step 2: Insert
                foreach (var idVoiceAssoc in buttonInfo.VoiceAssocID)
                {
                    AdDS.AppoimentRow appRow = DataSet.Appoiment.NewAppoimentRow();
                    appRow.IDVoiceAssoc = idVoiceAssoc;
                    appRow.Interval = buttonInfo.InvervalType.ToString();
                    appRow.Data = buttonInfo.Data;

                    DataSet.Appoiment.AddAppoimentRow(appRow);
                    DataSet.AcceptChanges();
                }
            }
        }
        public void DeleteAppoiment(AppoimentButtonInfo buttonInfo)
        {
            if (buttonInfo != null)
            {
                var appoimentRows = DataSet.Appoiment
                    .Where(item => item.Data.Date.Equals(buttonInfo.Data.Date)
                        && item.Interval.Equals(buttonInfo.InvervalType.ToString()))
                    .ToList();

                foreach (var appRow in appoimentRows)
                {
                    DataSet.Appoiment.RemoveAppoimentRow(appRow);
                    DataSet.AcceptChanges();
                }
            }
        }

        public List<VoiceBO> GetClientOccur(IntervalType intervalType)
        {
            List<VoiceBO> result = new List<VoiceBO>();

            if (intervalType == IntervalType.ALL)
            {
                var queryFromAppoiment = from ap in BusinessAccess.Instance.DataSet.Appoiment
                                         where
                                              ap.Data.Year == Common.AppCommon.SelectedMonth.Year
                                           && ap.Data.Month == Common.AppCommon.SelectedMonth.Month
                                         group ap by ap.VoiceClientAssocRow.ClientRow.Name into g
                                         select new VoiceBO
                                         {
                                             ClientName = g.Key,
                                             NoOccur = g.Count()
                                         };

                result = queryFromAppoiment.ToList();
            }
            else
            {

                var query = from ap in BusinessAccess.Instance.DataSet.Appoiment
                            where
                                 ap.Data.Year == Common.AppCommon.SelectedMonth.Year
                              && ap.Data.Month == Common.AppCommon.SelectedMonth.Month
                              && ap.Interval.Equals(intervalType.ToString())
                            group ap by ap.VoiceClientAssocRow.ClientRow.Name into g
                            select new VoiceBO
                            {
                                ClientName = g.Key,
                                NoOccur = g.Count()
                            };

                result = query.ToList();

            }

            return result;
        }

        #endregion





        public List<VoiceBO> GetVoiceList()
        {
            List<VoiceBO> voices = null;

            if (this.DataSet != null && this.DataSet.VoiceClientAssoc != null)
            {
                voices = this.DataSet.VoiceClientAssoc.Select().Select(row =>
                     new VoiceBO
                     {
                         IDVoiceAssoc = ((AdDS.VoiceClientAssocRow)row).ID,
                         VoiceID = ((AdDS.VoiceClientAssocRow)row).IDAdVoice,
                         ClientID = ((AdDS.VoiceClientAssocRow)row).IDClient,
                         VoiceName = ((AdDS.VoiceClientAssocRow)row).Description,
                         ClientName = ((AdDS.VoiceClientAssocRow)row).ClientRow.Name
                     }).ToList();
            }

            return voices;
        }

        public AdDS.AppoimentRow GetAppoimentForDate(DateTime data, IntervalType intervalType)
        {
            return this.DataSet.Appoiment
                .Where(
                    row => row.Data.Date.Equals(data.Date)
                //&& row.Interval == intervalType.ToString()
                    )
                .FirstOrDefault();
        }

        public Business.AdDS.VoiceClientAssocRow GetVoiceClientAssociations(int idVoiceAssoc)
        {
            //return this.DataSet.VoiceClientAssoc.Where(v => v.ID == idVoiceAssoc).FirstOrDefault();
            return this.DataSet.VoiceClientAssoc.FindByID(idVoiceAssoc);
        }

        public Business.AdDS.AppoimentRow GetAppoimentByIDAss(int idAppoiment)
        {
            return this.DataSet.Appoiment.FindByID(idAppoiment);
        }

        internal List<VoiceBO> GetNumarAparitiiClient(int idClient, out int numarAparitii)
        {
            ////get VoiceClient Associations
            //List<Business.AdDS.VoiceClientAssocRow> voiceClientAssoc = this.DataSet.VoiceClientAssoc
            //    .Where(v => v.ClientRow != null && v.ClientRow.ID == idClient)
            //    .ToList();

            ////get from Appoiment -> VoiceClientAssoc
            //List<Business.AdDS.AppoimentRow> currentAppoiments = this.DataSet.Appoiment
            //    .Where(row => row.Data.Year == AppCommon.SelectedMonth.Year && row.Data.Month == AppCommon.SelectedMonth.Month)
            //    .ToList();

            //int count = 0;
            //List<VoiceBO> voiceList = new List<VoiceBO>();
            //List<int> iduriAsoc = voiceClientAssoc.Select(item => item.ID).ToList();

            //foreach (var voiceClientAssocInApp in currentAppoiments)
            //{
            //    if (iduriAsoc.Contains(voiceClientAssocInApp.IDVoiceAssoc))
            //    {
            //        count++;

            //        var aparitii = this.DataSet.VoiceClientAssoc
            //             .Where(item => item.ID == voiceClientAssocInApp.IDVoiceAssoc)
            //             .FirstOrDefault();

            //        if (aparitii != null && aparitii.ClientRow != null)
            //        {
            //            voiceList.Add(new VoiceBO
            //            {
            //                VoiceName = GetVoiceClientAssociations(aparitii.IDAdVoice).Description,
            //                ClientName = aparitii.ClientRow.Name,
            //                Data = voiceClientAssocInApp.Data.ToString("dd-MMM-yyyy"),
            //                IntervalText = voiceClientAssocInApp.Interval
            //            });
            //        }
            //    }
            //}

            ////foreach (var voiceClientAssocInApp in currentAppoiments)
            ////{
            ////    if (voiceClientAssoc.Contains(voiceClientAssocInApp))
            ////    {
            ////        count++;

            ////        var aparitii = this.DataSet.VoiceClientAssoc
            ////            .Where(item => item.ID == voiceClientAssocInApp)
            ////            .FirstOrDefault();

            ////        if (aparitii != null && aparitii.ClientRow != null)
            ////        {
            ////            voiceList.Add(new VoiceBO
            ////            {
            ////                VoiceName = GetVoiceClientAssociations(aparitii.IDAdVoice).Description,
            ////                ClientName = aparitii.ClientRow.Name,
            ////                Data = 
            ////                //Data = aparitii.Data
            ////            });
            ////        }
            ////    }
            ////}

            //numarAparitii = count;
            //return voiceList;

            numarAparitii = 0;
            return null;
        }




    }
}
